package com.cskaoyan.controller;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CheckoutVo;
import com.cskaoyan.domain.MarketCart;
import com.cskaoyan.domain.MarketGoodsProduct;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.mapper.MarketCartMapper;
import com.cskaoyan.service.CartService;
import com.cskaoyan.service.CartServiceImpl;
import com.cskaoyan.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/cart")
public class WxCartController {
    @Autowired
    CartService cartService;
    @Autowired
    GoodsService goodsService;
    @GetMapping("goodscount")
    public BaseRespVo goodsCount() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId=marketUser.getId();
        Integer data = cartService.getGoodsCountByUserId(userId);
        return BaseRespVo.ok(data);
    }
    @PostMapping("add")
    public BaseRespVo cartAdd(@RequestBody Map map) {
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        Integer userId=1;
        BaseRespVo baseRespVo = cartService.addCartGoods(goodsId, userId, number, productId);
        return baseRespVo;
    }

    @GetMapping("index")
    public BaseRespVo getIndex() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        BaseRespVo baseRespVo = cartService.selectCartIndexByUserId(userId);
        return baseRespVo;
    }
    @PostMapping("checked")
    public BaseRespVo cartChecked(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId=marketUser.getId();
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        Integer isChecked = (Integer) map.get("isChecked");
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        BaseRespVo baseRespVo = cartService.checkCart(isChecked, productIds, userId);
        return baseRespVo;
    }
    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId=marketUser.getId();
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        BaseRespVo baseRespVo = cartService.fastAddCartGoods(goodsId, userId, number, productId);
        return baseRespVo;
    }

    @GetMapping("checkout")
    public BaseRespVo checkout(CheckoutVo checkoutVo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId=marketUser.getId();
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        BaseRespVo baseRespVo=cartService.getCartCheckout(checkoutVo,userId);
        return baseRespVo;
    }

    @RequestMapping("update")
    public BaseRespVo cartUpdate(@RequestBody Map map){
        MarketCart marketCart = new MarketCart();
        Integer number = (Integer) map.get("number");
        short i1 = number.shortValue();
        marketCart.setNumber(i1);
        Integer goodsId = (Integer) map.get("goodsId");
        int i = cartService.updateNumber(marketCart, goodsId);

        List<MarketGoodsProduct> productById = goodsService.getProductById(goodsId);
        MarketGoodsProduct marketGoodsProduct = productById.get(0);
        Integer number1 = marketGoodsProduct.getNumber();
        return BaseRespVo.ok();

    }
    @RequestMapping("delete")
    public BaseRespVo deleteCart(@RequestBody Map map){
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        MarketCart marketCart = new MarketCart();
        marketCart.setDeleted(true);
        for (Integer productId : productIds) {
            int i = cartService.updateDeleted(marketCart,productId);
        }
        return this.getIndex();
    }
}